local skel = fk.CreateSkill {
  name = "rmt__daigou",
  tags = {Skill.Limited},
}

local daigou_pool = {}

-- 带狗身份局池子
daigou_pool[1] = {
  "steam__godhanxin", -- 蒸神韩信
  "steam__corrupted_bluebaby", -- 蒸堕化蓝人
  "steam__hollowknight", -- 蒸空洞骑士
  "steam__lazarus", -- 蒸拉撒路
  "steam__keeper", -- 蒸店长
  "rmt__murongke", -- 封侯慕容恪
  "moe__heimei", -- 萌包黑梅
  "steam__isaac", -- 蒸以撒
  "steam__nasus", -- 蒸内瑟斯
  "efengqi__godlvbu", -- 风起神吕布

  "steam__apollyon", -- 蒸亚玻伦
  "steam__trump", -- 蒸特朗普
  "steam__liuyan", -- 蒸刘焉
  "steam__zhugdan", -- 蒸诸葛诞
  "efengqi__caopi", -- 风起曹丕
  "steam__corrupted_apollyon", -- 蒸堕化亚玻伦

  "rmt__yingzheng", -- 封侯嬴政
  "steam__wenyang", -- 蒸文鸯（5血决斗哥）
  "steam__corrupted_eve", -- 蒸堕夏娃
  "rmt__shihu", -- 封侯石虎
  "steam__sunhao", -- 蒸孙皓
  "rmt__liuche", -- 封侯刘彻
  "tyj__abathur", -- 汤圆阿巴瑟
  "steam__chengtang", -- 蒸成汤
  "steam__jifa", -- 蒸姬发

  -- 第一代老东西
  "rmt__huanwen", -- 封侯桓温（浴霸哥）
  "steam__thutmose_third", -- 蒸法老，连击摸8哥
  "efengqi__zhangfei", -- 风起张飞
  "steam__azazel", --蒸阿撒泻勒（印兵临哥）
  "rmt__yuanziyou", -- 封侯元子攸（伏案双限定哥）
  "rmt__zhangxianzhong", -- 封侯张献忠
  "wanghe", -- 王龁
  "xiaobai-eleven__zhushixing", -- 小白朱士行（和尚哥）
  "efengqi__zhangliao", -- 风起张辽
  "rmt__jisi", -- 封侯先帝祭祀

  -- 老东西
  "rmt__simawei", -- 封侯司马玮
  "efengqi__luxun", -- 风起陆逊
  "efengqi__sunquan", -- 风起孙权
  "rmt__yangfenghanxian", -- 封侯杨奉韩暹
  "xiaobai__lishu", -- 小白李术
  "rmt__wangling", -- 封侯王凌
  "efengqi__caocao", -- 风起曹操
  "godwangjian", -- 神王翦
  "rmt__miaomiao", -- 封侯妙妙
}

-- 带狗斗地主池子
daigou_pool[2] = {
  "efengqi__guanyu", -- 风起关羽
  "rmt__yujig", -- 风起虞姬
  "steam__hornet", -- 蒸大黄蜂 
  "xiaobai-ten__fugu", -- 小白傅嘏
  "pangjuan", -- 汗青庞涓
  "tyj__hansui", -- 汤圆韩遂
  "ysch4__zhouxuan", -- 亦书成花周宣
  "ysch4__Jinx", -- 亦书成花金克丝
  "steam__godhanxin", -- 蒸神韩信
  "steam__godxunyu", -- 蒸神荀彧

  "rmt__th_kogasa", -- 封侯R神
  "xiaobai-eleven__lizhaoyi", -- 小白李昭仪
  "steam__shitao", -- 蒸石韬
  "steam__laizhonghui", -- 蒸来钟会
  "efengqi__liubei", -- 风起刘备
  "steam__jvshou", -- 蒸沮授

  "wanghe", -- 汗青王龁
  "sa__dianwei", -- S赛典韦
  "tea__zhangwen", -- 茶水张温
  "steam__liuchen", -- 蒸刘谌
  "moesp__xiajie", -- 萌夏桀
  "godlikui", -- 汗青神李悝
  "blitz__Thirty_Seven", -- 提瓦特闪击37
  "rmt__nero", -- 封侯尼禄
  "rmt__wangling", -- 封侯王凌
  "xiaobai-eleven__weidan", -- 小白韦诞
  "rmt__jisi", -- 封侯先帝祭祀
  "steam__simashi", -- 蒸司马师
  "ol__feiyi", -- OL费祎
}

-- 带狗2v2团战2池子
daigou_pool[3] = {
  "efengqi__lukang", -- 风起陆抗
  "xiaobai__zhugerong", -- 小白诸葛融
  "efengqi__luxun", -- 风起陆逊
  "efengqi__guanyu", -- 风起关羽
  "rmt__simawei", -- 封侯司马玮
  "xiaobai__jiyan", -- 小白暨艳（规矩哥）
  -- 牢R关小黑屋 "rmt__th_kogasa", -- 封侯R神
  "rmt__wangling", -- 封侯王凌
  "rmt__nero", -- 封侯尼禄
  "tiandan", -- 田单

  -- 老东西
  "xiaobai-ten__fugu", -- 小白傅嘏
  "efengqi__guojia", -- 风起郭嘉
  "rmt__xiangchong", -- 封侯向宠
  "efengqi__zhangliao", -- 风起张辽
  "rmt__caozhao", -- 封侯曹肇
}



--- 获取带狗池子并公示
---@param room Room
---@return string[]
local getDaigouPool = function(room)
  local excludeGnerals = {}
  for _, p in ipairs(room.players) do
    table.insert(excludeGnerals, p.general)
    table.insert(excludeGnerals, p.deputyGeneral)
  end
  local index = 1 -- 默认身份模式池
  if  room:isGameMode("1v2_mode") then -- 2：斗地主
    index = 2
  elseif room:isGameMode("2v2_mode") then -- 3：22团战
    index = 3
  end
  local generals = daigou_pool[index] or {}
  generals = table.filter(generals, function(name) return Fk.generals[name] ~= nil and not table.contains(excludeGnerals, name) end)
  -- 是否仅能从房间开启武将中获取？ true 为开启禁表，false 为关闭
  local openBanList = false
  if openBanList then
    generals = table.filter(generals, function(name) return Fk:canUseGeneral(name) end)
  end
  local ret = {} -- 筛选重复
  for _, name in ipairs(generals) do
    table.insertIfNeed(ret, name)
  end
  ret = table.slice(ret, 1, 11) -- 取存在的武将前10个
  if room:getBanner("@&rmt__daigou") == nil then
    room:setBanner("@&rmt__daigou", ret)
  end
  return ret
end

skel:addAcquireEffect(function (self, player, is_start)
  getDaigouPool(player.room)
end)

skel:addEffect("active", {
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  prompt = "#rmt__daigou",
  target_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local generals = getDaigouPool(room)
    if #generals == 0 then return end
    local orig = to.general
    local general = table.random(generals)
    room:findGeneral(general)
    room:changeHero(to, general, false, false, true, true, true)
    if to.general == general then -- 预防一下插结乱改武将
      to.tag["rmt__daigou_general"] = general
      to.tag["rmt__daigou_orig"] = orig
      to:chat(Fk:translate(general).."带条狗都能赢")
    end
  end,
})

skel:addEffect(fk.GameStart, {
  priority = 1.01, -- 优先于游戏开始时的其他技能
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = player.room.alive_players, skill_name = skel.name, prompt = "#rmt__daigou",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos} )
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local active = Fk.skills[skel.name]
    if active and active:isInstanceOf(ActiveSkill) then
      active:onUse(player.room, { from = player, tos = event:getCostData(self).tos , cards = {} })
    end
  end,
})

-- 仅用于发送骚话
skel:addEffect(fk.BeforeGameOverJudge, {
  can_refresh = function (self, event, target, player, data)
    local generalRecord = player.tag["rmt__daigou_general"]
    if generalRecord == nil then return false end
    return data.damage and data.damage.from == player
  end,
  on_refresh = function (self, event, target, player, data)
    local general = player.general
    local generalRecord = player.tag["rmt__daigou_general"]
    local nametrans = Fk:translate(generalRecord)
    if general == generalRecord then
      target:chat("狗设计的！")
    else
      target:chat(nametrans .."真是狗设计的！")
    end
  end,
})

-- 带狗武将被杀死或造成3点伤害后，受伤角色怒骂道
skel:addEffect(fk.Damaged, {
  can_refresh = function (self, event, target, player, data)
    local generalRecord = player.tag["rmt__daigou_general"]
    if generalRecord == nil then return false end
    return data.from == player and data.damage > 2 and not target.dead
  end,
  on_refresh = function (self, event, target, player, data)
    local general = player.general
    local generalRecord = player.tag["rmt__daigou_general"]
    local nametrans = Fk:translate(generalRecord)
    if general == generalRecord then
      target:chat("狗设计的！")
    else
      target:chat(nametrans .."真是狗设计的！")
    end
  end,
})

skel:addEffect(fk.AfterPropertyChange, {
  anim_type = "drawcard",
  can_refresh = function(self, event, target, player, data)
    local generalRecord = player.tag["rmt__daigou_general"]
    if generalRecord == nil then return false end
    return target == player and data.results and data.results["generalChange"] and data.results["generalChange"][1] == generalRecord
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local generalRecord = player.tag["rmt__daigou_general"]
    local nametrans = Fk:translate(generalRecord)
    -- 由带狗武将变为其他武将时发送
    player:chat("保留我的"..nametrans.."吧")
  end,
})

-- 游戏结算时，将变更武将还原
skel:addEffect(fk.GameFinished, {
  can_refresh = function(self, event, target, player, data)
    return player.tag["rmt__daigou_general"] ~= nil
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local generalRecord = player.tag["rmt__daigou_general"]
    local nametrans = Fk:translate(generalRecord)
    -- 游戏结束时，切换为原武将（为了胜率统计
    local orig = player.tag["rmt__daigou_orig"]
    if orig then
      room:setPlayerProperty(player, "general", orig)
      -- 如果赢了，则得意
      if data and type(data) == "string" then
        if string.find(data, player.role) then
          player:chat(nametrans .. " 比 " .. Fk:translate(orig) .. " 强多了!")
        else
          player:chat("感觉 " .. nametrans .." 被严重高估…")
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__daigou"] = "带狗",
  [":rmt__daigou"] = "限定技，游戏开始时，或出牌阶段，你可以令一名角色变成“xx带条狗都能赢”池子中的随机一名武将。",

  ["#rmt__daigou"] = "带狗：令一名角色变成“xx带条狗都能赢”池子中一名武将",
  ["@&rmt__daigou"] = "带狗将池",
}

return skel
